/**
 * 
 */
package com.jbat.gwt.tiledmapcreator.client.activities;

import com.jbat.gwt.tiledmapcreator.client.ImageLocatorService;
import com.jbat.gwt.tiledmapcreator.client.ImageLocatorServiceAsync;
import com.jbat.gwt.tiledmapcreator.client.NoSuchDirectoryException;
import com.jbat.gwt.tiledmapcreator.client.places.MapDesignPlace;
import com.jbat.gwt.tiledmapcreator.client.views.TileSetView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 */
public class TileSetActivity extends AbstractActivity implements TileSetView.Presenter {
  private String tileSetName;
  private TileSetView view;

  private ImageLocatorServiceAsync imageLocatorSvc = GWT.create(ImageLocatorService.class);

  @Inject
  public TileSetActivity(TileSetView view, @Assisted MapDesignPlace place) {
    this.view = view;
    tileSetName = place.getTileSetName();
  }

  public void getTileSetImages() {
    // Initialize the service proxy
    if (imageLocatorSvc == null) {
      imageLocatorSvc = GWT.create(ImageLocatorService.class);
    }

    // Set up the callback object.
    AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable caught) {
        // If there is no such tile set throw an error
        String details = caught.getMessage();
        if (caught instanceof NoSuchDirectoryException) {
          details = "Tile Set '" + tileSetName + "' was not found or has no images.";
        }

        // TODO appropriately log this message
        GWT.log(details);
      }

      @Override
      public void onSuccess(List<String> result) {
        setData(result);
      }
    };

    // Make the call to the image locator service
    imageLocatorSvc.getImageNames(tileSetName, GWT.getModuleName(), callback);
  }

  /**
   * Takes a list of fileNames, correctly prepends the url and tileset information, then sends this
   * list to the view to generate tiles of images.
   * 
   * @param fileNames
   */
  public void setData(List<String> fileNames) {
    List<String> adjustedFileNames = new ArrayList<String>();
    for (String fileName : fileNames) {
      // prepend URL location info to every file name
      String adustedfileName = GWT.getModuleBaseForStaticFiles() + tileSetName + "/" + fileName;
      adjustedFileNames.add(adustedfileName);
    }
    view.setData(adjustedFileNames);
  }

  /*
   * (non-Javadoc)
   * 
   * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client
   * .ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
   */
  @Override
  public void start(AcceptsOneWidget panel, EventBus eventBus) {
    // TODO get list of image names in tileset folder from server
    getTileSetImages();
    view.setPresenter(this);
    panel.setWidget(view);
  }

}
